﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;


namespace lxwmvc
{
    /// <summary>
    /// 单表的crud操作
    /// </summary>
    public static class tablecontroller
    {
        public static void _init(pagebase pb)
        {
            //pb["syshelper"] = new syshelper();

            //if (cookieshelper.getcookie("m_quanxian").toint32() < 2) { throw new Exception("非法的访问！"); }

            //pb.settable("tablename");
        }

        /// <summary>
        /// 增加或者编辑页面
        /// </summary>
        /// <param name="pb"></param>
        [layout("default")]
        public static void add(pagebase pb)
        {
            string sql = "";
            int idv = pb.v_i(pb.pk);
            if (idv > 0)
            {
                sql = "select * from " + pb.table + " where " + pb.pk + "=" + idv;
                DataSet ds = sqlhelper.has(pb, sql);
                pb.autoloaddata(ds);


            }
            pb["formurl"] = requesthelper.formurl();
        }

        [layout("default")]
        [trans]
        public static void list(pagebase pb)
        {
            string flag = pb.v_s("flag").ToLower();
            int idv = pb.v_i(pb.pk);
            if (flag == "delete")
            {
                sqlhelper.execute("delete from " + pb.table + " where " + pb.pk + "=" + idv);
                //jshelper.echo("alert('删除成功!');parent.location.href=parent.location.href;");
                pb.cancelview();

                //得到关联信息
                //子表_子表主键_关联主表键
                if (pb.hasjilian)
                {
                    string[] jilians = pb.jilians;
                    string sql = "delete from " + jilians[0] + " where " + jilians[2] + "=" + idv;
                    sqlhelper.execute(sql);
                }

            }
            else if (flag == "deleteall")
            {
                sqlhelper.execute("delete from " + pb.table + " where " + pb.pk + " in(" + pb.v_s(pb.pk) + ")");
                //jshelper.echo("alert('删除成功!');parent.location.href=parent.location.href;");
                pb.cancelview();

                if (pb.hasjilian)
                {
                    string[] jilians = pb.jilians;
                    string sql = "delete from " + jilians[0] + " where " + jilians[2] + " in(" + pb.v_s(pb.pk) + ")";
                    sqlhelper.execute(sql);
                }
            }
            else if (flag == "copy")
            {
                lxwdata has = sqlhelper.has("select * from " + pb.table + " where " + pb.pk + "=" + idv);
                has.Remove(pb.pk);

                result ret = sqlhelper.createorupdate(has, pb.pk, pb.table);
                pb.cancelview();

                if (pb.hasjilian)
                {
                    string[] jilians = pb.jilians;
                    var drs = queryhelper.rows(" select * from " + jilians[0] + " where " + jilians[2] + "=" + idv);
                    foreach (DataRow dr in drs)
                    {
                        has = sqlhelper.has(dr);
                        has[jilians[2]] = ret.identity;
                        has.Remove(jilians[1]);

                        sqlhelper.createorupdate(has, jilians[1], jilians[0]);
                    }
                }
            }
            else if (flag == "copyall")
            {
                foreach (string s in pb.v_s(pb.pk).Split(','))
                {
                    if (s == "-1") continue;
                    lxwdata has = sqlhelper.has("select * from " + pb.table + " where " + pb.pk + "=" + s);
                    has.Remove(pb.pk);

                    result ret = sqlhelper.createorupdate(has, pb.pk, pb.table);

                    if (pb.hasjilian)
                    {
                        string[] jilians = pb.jilians;
                        var drs = queryhelper.rows(" select * from " + jilians[0] + " where " + jilians[2] + "=" + s);
                        foreach (DataRow dr in drs)
                        {
                            has = sqlhelper.has(dr);
                            has[jilians[2]] = ret.identity;
                            has.Remove(jilians[1]);

                            sqlhelper.createorupdate(has, jilians[1], jilians[0]);
                        }
                    }
                }
                pb.cancelview();
            }
            else
            {
                string where = pb.search();
                string orderby = pb.pk + " asc";
                {
                    //前面传递的排序信息
                    //cn 和 ot，如果这两个有值，就直接用着两个来组合
                    string cn = pb.orderbycolumn(pb.pk, pb.ordertype());
                    if (!string.IsNullOrEmpty(cn))
                        orderby = cn;

                    //代表外部设置了排序,只用于第一次
                    if (pb.v_s("orderby").Length > 0)
                        orderby = pb.paramsvalue("orderby", false);
                }

                sqlhelper.showpager(pb, pb.table, "*", orderby, pb.page, pb.pagesize, where);
            }
        }

        /// <summary>
        /// 不用修改程序就能添加字段，添加业务
        /// </summary>
        /// <param name="pb"></param>
        [trans]
        public static void save(pagebase pb)
        {
            pb.cancelview();

            checkbuchongfu(pb);


            result result = sqlhelper.savetable(pb, pb.pk, pb.table, delegate(lxwdata has)
            {

            }, null);


            string iframe = pb.paramsvalue("_ajax_submit_");
            string g_msg = pb.paramsvalue("g_msg");


            if (iframe == "1")
            {
                if (g_msg != "")
                {
                    //alert('保存成功');
                    pb.write(g_msg.Replace("&#39;&#39;", "'"));
                }
                else
                {
                    //alert('保存成功');
                    pb.write("location.href='" + pb.formurl + "';");
                }
            }
            else
            {
                string url = pb.formurl;
                if (url.ToLower() == "add") url += "?furl=add";

                if (g_msg != "")
                {
                    //alert('保存成功');
                    jshelper.alert(g_msg.Replace("&#39;&#39;", "'"));
                }
                else
                {
                    //alert('保存成功');
                    jshelper.echo("parent.location.href='" + url + "'; ");
                }
            }
        }

        /// <summary>
        /// 判断不重复
        /// </summary>
        /// <param name="pb"></param>
        private static void checkbuchongfu(pagebase pb)
        {
            string tishierrmsg = "请给验证唯一性的字段赋值err信息！";

            //唯一性校验
            string buchongfu = pb.paramsvalue("_lxw_column_buchongfu_");
            if (buchongfu.Length > 0)
            {
                string[] nodes = buchongfu.Split(',');
                string[] msg = pb.paramsvalue("_lxw_column_buchongfu_msg_").Split(',');
                for (int i = 0; i < nodes.Length; i++)
                {
                    if (string.IsNullOrEmpty(nodes[i])) continue;
                    sqlhelper.weiyixing(
                        pb.table,
                        nodes[i],
                        pb.paramsvalue(nodes[i]),
                        msg.Length > i ? (msg[i] == "" ? tishierrmsg : msg[i]) : tishierrmsg,
                        pb.pk,
                        pb.paramsvalue(pb.pk)

                        );
                }
            }
        }
    }
}